package org.example;

import com.googlecode.aviator.AviatorEvaluator;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * Google Aviator BigDecimal 使用示例
 * 展示如何在Aviator中使用BigDecimal进行高精度计算
 */
public class AviatorBigDecimalDemo {

    public static void main(String[] args) {
        System.out.println("=== Google Aviator BigDecimal 使用示例 ===\n");


        // 2. 带变量的BigDecimal表达式
        variableBigDecimalExpression();


        // 4. 财务计算示例
        financialCalculationExample();

    }



    /**
     * 2. 带变量的BigDecimal表达式
     */
    private static void variableBigDecimalExpression() {
        System.out.println("2. 带变量的BigDecimal表达式");

        try {
            for (int i = 0; i < 100000; i++) {
            System.out.println("--------------------------");

            // 创建参数Map
            Map<String, Object> params = new HashMap<>();
            params.put("price", new BigDecimal("99.99"));
            params.put("quantity", new BigDecimal("5"));
            params.put("taxRate", new BigDecimal("0.06"));
            params.put("discount", new BigDecimal("0.10"));
            // 留出预留的部分【可配置】
            params.put("one", new BigDecimal("1"));
            params.put("two", new BigDecimal("2"));
            params.put("three", new BigDecimal("3"));
            params.put("four", new BigDecimal("4"));
            params.put("five", new BigDecimal("5"));
            params.put("six", new BigDecimal("6"));
            params.put("seven", new BigDecimal("7"));
            params.put("eight", new BigDecimal("8"));
            params.put("nie", new BigDecimal("9"));
            params.put("ten", new BigDecimal("10"));

            // 商品总价计算：价格 * 数量 * (1 - 折扣) * (1 + 税率)
            String expression = "price * quantity * (one-discount) * (one + taxRate)";
            BigDecimal total = (BigDecimal) AviatorEvaluator.execute(expression, params, true);

            System.out.println("商品价格: " + params.get("price"));
            System.out.println("购买数量: " + params.get("quantity"));
            System.out.println("税率: " + params.get("taxRate"));
            System.out.println("折扣: " + params.get("discount"));
            System.out.println("最终总价: " + total.setScale(2, RoundingMode.HALF_UP));

            System.out.println();


            }
        } catch (Exception e) {
            System.err.println("计算错误: " + e.getMessage());
        }
    }


    /**
     * 4. 财务计算示例【贷款】
     */
    private static void financialCalculationExample() {
        System.out.println("4. 财务计算示例【贷款】");
        System.out.println("----------------");

        try {
            // 贷款计算参数
            Map<String, Object> loanParams = new HashMap<>();
            loanParams.put("principal", new BigDecimal("1000000")); // 本金100万
            loanParams.put("annualRate", new BigDecimal("0.049")); // 年利率4.9%【固定利率，不是LPR利率】
            loanParams.put("period", new BigDecimal("240")); // 贷款期限240个月
            loanParams.put("months", new BigDecimal("12")); // 12个月
            // 留出预留的部分【可配置】
            loanParams.put("one", new BigDecimal("1"));
            loanParams.put("two", new BigDecimal("2"));
            loanParams.put("three", new BigDecimal("3"));
            loanParams.put("four", new BigDecimal("4"));
            loanParams.put("five", new BigDecimal("5"));
            loanParams.put("six", new BigDecimal("6"));
            loanParams.put("seven", new BigDecimal("7"));
            loanParams.put("eight", new BigDecimal("8"));
            loanParams.put("nie", new BigDecimal("9"));
            loanParams.put("ten", new BigDecimal("10"));

            // 月供计算公式: 月供 = 本金 * 月利率 * (1 + 月利率)^期数 / [(1 + 月利率)^期数 - 1]
            String monthlyPaymentExpr = "principal * (annualRate / months) * " +
                    "((one + (annualRate / months))**(period)) / " +
                    "(((one + (annualRate / months))**(period)) - one)";

            BigDecimal monthlyPayment = (BigDecimal) AviatorEvaluator.execute(monthlyPaymentExpr, loanParams, true);
            BigDecimal totalPayment = monthlyPayment.multiply((BigDecimal) loanParams.get("period"));
            BigDecimal totalInterest = totalPayment.subtract((BigDecimal) loanParams.get("principal"));

            System.out.println("贷款本金: " + loanParams.get("principal"));
            System.out.println("年利率: " + loanParams.get("annualRate"));
            System.out.println("贷款期限: " + loanParams.get("period") + "个月");
            System.out.println("月供金额: " + monthlyPayment.setScale(2, RoundingMode.HALF_UP));
            System.out.println("还款总额: " + totalPayment.setScale(2, RoundingMode.HALF_UP));
            System.out.println("支付利息: " + totalInterest.setScale(2, RoundingMode.HALF_UP));

            System.out.println();
        } catch (Exception e) {
            System.err.println("财务计算错误: " + e.getMessage());
        }
    }


}